{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 导入TensorFlow等"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T11:31:09.163891Z",
     "start_time": "2018-05-07T11:31:08.473467Z"
    },
    "nbpresent": {
     "id": "35a12da4-0c55-40d8-b81a-55e8ac4ca4fd"
    }
   },
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "# import pandas as pd\n",
    "\n",
    "# import matplotlib.pyplot as plt\n",
    "# %matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 定义神经网络函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T11:31:10.167293Z",
     "start_time": "2018-05-07T11:31:10.148340Z"
    }
   },
   "outputs": [],
   "source": [
    "def add_layer(inputs, in_size, out_size, num=0, activation_function=None):\n",
    "    # add one more layer and return the output of this layer\n",
    "    # 区别：大框架，定义层 layer，里面有 小部件\n",
    "    \n",
    "    with tf.name_scope('layer'):\n",
    "        # 区别：小部件\n",
    "        with tf.name_scope('weights'):\n",
    "            Weights = tf.Variable(tf.random_normal([in_size, out_size]), name='Weights' + str(num))\n",
    "        with tf.name_scope('biases'):\n",
    "            biases = tf.Variable(tf.zeros([1, out_size]) + 0.1, name='bias' + str(num))\n",
    "        with tf.name_scope('Wx_plus_b'):\n",
    "            Wx_plus_b = tf.add(tf.matmul(inputs, Weights), biases, name='We_plus_b' + str(num))\n",
    "        if activation_function is None:\n",
    "            outputs = Wx_plus_b\n",
    "        else:\n",
    "            outputs = activation_function(Wx_plus_b, name='activation_function' + str(num))\n",
    "        return outputs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 烟雾检测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T11:31:11.630233Z",
     "start_time": "2018-05-07T11:31:11.596009Z"
    }
   },
   "outputs": [],
   "source": [
    "import cv2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 读取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T11:31:15.264451Z",
     "start_time": "2018-05-07T11:31:12.380077Z"
    }
   },
   "outputs": [],
   "source": [
    "training_set = [np.reshape(cv2.cvtColor(cv2.resize(cv2.imread('test8/%06d.png' % i), (256, 256)), cv2.COLOR_BGR2GRAY), (1, 256 * 256)) for i in range(1, 401)]\n",
    "training_set.extend([np.reshape(cv2.cvtColor(cv2.resize(cv2.imread('test8/%06d.png' % i), (256, 256)), cv2.COLOR_BGR2GRAY), (1, 256 * 256)) for i in range(501, 901)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T11:31:15.409371Z",
     "start_time": "2018-05-07T11:31:15.384979Z"
    }
   },
   "outputs": [],
   "source": [
    "training_set_nparray = np.vstack(training_set)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T11:31:15.539238Z",
     "start_time": "2018-05-07T11:31:15.536553Z"
    }
   },
   "outputs": [],
   "source": [
    "training_label = np.array([[1.0 if i < 400 else 0.0] for i in range(800)])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 定义输入输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T11:31:15.701225Z",
     "start_time": "2018-05-07T11:31:15.683203Z"
    }
   },
   "outputs": [],
   "source": [
    "with tf.name_scope('inputs'):\n",
    "    xs = tf.placeholder(tf.float32, [None, 256 * 256], name='xs')\n",
    "    ys = tf.placeholder(tf.float32, [None, 1], name='ys')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 定义隐层和输出层"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T11:31:15.873994Z",
     "start_time": "2018-05-07T11:31:15.855856Z"
    }
   },
   "outputs": [],
   "source": [
    "layer1 = add_layer(xs, 256 * 256, 1024, 1, activation_function=tf.nn.sigmoid)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T11:31:16.063889Z",
     "start_time": "2018-05-07T11:31:16.053612Z"
    }
   },
   "outputs": [],
   "source": [
    "prediction = add_layer(layer1, 1024, 1, 2, activation_function=tf.nn.sigmoid)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 定义损失函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T11:31:16.617435Z",
     "start_time": "2018-05-07T11:31:16.611857Z"
    }
   },
   "outputs": [],
   "source": [
    "with tf.name_scope('loss'):\n",
    "    loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction), reduction_indices=[1]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 使用全局梯度下降函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T11:31:17.371485Z",
     "start_time": "2018-05-07T11:31:17.326528Z"
    }
   },
   "outputs": [],
   "source": [
    "with tf.name_scope('train'):\n",
    "    train_step = tf.train.GradientDescentOptimizer(1).minimize(loss)\n",
    "# train_step = tf.train.AdamOptimizer().minimize(loss)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T11:31:17.666578Z",
     "start_time": "2018-05-07T11:31:17.661732Z"
    }
   },
   "outputs": [],
   "source": [
    "smoke_sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T11:31:20.849192Z",
     "start_time": "2018-05-07T11:31:20.765871Z"
    }
   },
   "outputs": [],
   "source": [
    "writer = tf.summary.FileWriter(\"logs/\", smoke_sess.graph)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 初始化变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T11:31:22.495302Z",
     "start_time": "2018-05-07T11:31:21.874098Z"
    }
   },
   "outputs": [],
   "source": [
    "smoke_sess.run(tf.global_variables_initializer())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 训练模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T12:05:59.130087Z",
     "start_time": "2018-05-07T11:31:25.078853Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 0.490466\n",
      "50 0.480594\n",
      "100 0.475808\n",
      "150 0.414057\n",
      "200 0.377545\n",
      "250 0.360905\n",
      "300 0.343807\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-15-858121e3affe>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1000\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m     \u001b[0msmoke_sess\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtrain_step\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfeed_dict\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m{\u001b[0m\u001b[0mxs\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mtraining_set_nparray\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mys\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mtraining_label\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      3\u001b[0m     \u001b[0;32mif\u001b[0m \u001b[0mi\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0;36m50\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m         \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msmoke_sess\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mloss\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfeed_dict\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m{\u001b[0m\u001b[0mxs\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mtraining_set_nparray\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mys\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mtraining_label\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py\u001b[0m in \u001b[0;36mrun\u001b[0;34m(self, fetches, feed_dict, options, run_metadata)\u001b[0m\n\u001b[1;32m    893\u001b[0m     \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    894\u001b[0m       result = self._run(None, fetches, feed_dict, options_ptr,\n\u001b[0;32m--> 895\u001b[0;31m                          run_metadata_ptr)\n\u001b[0m\u001b[1;32m    896\u001b[0m       \u001b[0;32mif\u001b[0m \u001b[0mrun_metadata\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    897\u001b[0m         \u001b[0mproto_data\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtf_session\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mTF_GetBuffer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrun_metadata_ptr\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py\u001b[0m in \u001b[0;36m_run\u001b[0;34m(self, handle, fetches, feed_dict, options, run_metadata)\u001b[0m\n\u001b[1;32m   1122\u001b[0m     \u001b[0;32mif\u001b[0m \u001b[0mfinal_fetches\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0mfinal_targets\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mhandle\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mfeed_dict_tensor\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1123\u001b[0m       results = self._do_run(handle, final_targets, final_fetches,\n\u001b[0;32m-> 1124\u001b[0;31m                              feed_dict_tensor, options, run_metadata)\n\u001b[0m\u001b[1;32m   1125\u001b[0m     \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1126\u001b[0m       \u001b[0mresults\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py\u001b[0m in \u001b[0;36m_do_run\u001b[0;34m(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)\u001b[0m\n\u001b[1;32m   1319\u001b[0m     \u001b[0;32mif\u001b[0m \u001b[0mhandle\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1320\u001b[0m       return self._do_call(_run_fn, self._session, feeds, fetches, targets,\n\u001b[0;32m-> 1321\u001b[0;31m                            options, run_metadata)\n\u001b[0m\u001b[1;32m   1322\u001b[0m     \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1323\u001b[0m       \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_do_call\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0m_prun_fn\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_session\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mhandle\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfeeds\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfetches\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py\u001b[0m in \u001b[0;36m_do_call\u001b[0;34m(self, fn, *args)\u001b[0m\n\u001b[1;32m   1325\u001b[0m   \u001b[0;32mdef\u001b[0m \u001b[0m_do_call\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfn\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1326\u001b[0m     \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1327\u001b[0;31m       \u001b[0;32mreturn\u001b[0m \u001b[0mfn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   1328\u001b[0m     \u001b[0;32mexcept\u001b[0m \u001b[0merrors\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mOpError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1329\u001b[0m       \u001b[0mmessage\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcompat\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mas_text\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0me\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmessage\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py\u001b[0m in \u001b[0;36m_run_fn\u001b[0;34m(session, feed_dict, fetch_list, target_list, options, run_metadata)\u001b[0m\n\u001b[1;32m   1304\u001b[0m           return tf_session.TF_Run(session, options,\n\u001b[1;32m   1305\u001b[0m                                    \u001b[0mfeed_dict\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfetch_list\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtarget_list\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1306\u001b[0;31m                                    status, run_metadata)\n\u001b[0m\u001b[1;32m   1307\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1308\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0m_prun_fn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msession\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mhandle\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfeed_dict\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfetch_list\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "for i in range(1000):\n",
    "    smoke_sess.run(train_step, feed_dict={xs: training_set_nparray, ys: training_label})\n",
    "    if i % 50 == 0:\n",
    "        print(i, smoke_sess.run(loss, feed_dict={xs: training_set_nparray, ys: training_label}))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 在训练集上的结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T12:05:59.132121Z",
     "start_time": "2018-05-07T11:31:28.136Z"
    }
   },
   "outputs": [],
   "source": [
    "training_set_prediction = smoke_sess.run(prediction, feed_dict={xs: training_set_nparray})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T12:05:59.134633Z",
     "start_time": "2018-05-07T11:31:28.712Z"
    }
   },
   "outputs": [],
   "source": [
    "training_set_prediction[training_set_prediction >= 0.5] = 1\n",
    "training_set_prediction[training_set_prediction < 0.5] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T12:05:59.136661Z",
     "start_time": "2018-05-07T11:31:28.986Z"
    }
   },
   "outputs": [],
   "source": [
    "training_set_prediction_positive = training_set_prediction[: 400]\n",
    "training_set_prediction_negative = training_set_prediction[400:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T12:05:59.138486Z",
     "start_time": "2018-05-07T11:31:31.028Z"
    }
   },
   "outputs": [],
   "source": [
    "print('FC Training Set Confusion Matrix\\n          Ture      False')\n",
    "\n",
    "print('Positive： {0}        {1}'.format(\n",
    "    len(training_set_prediction_positive[training_set_prediction_positive == 1]),len(training_set_prediction_positive[training_set_prediction_positive == 0])))\n",
    "print('Negative： {0}        {1}'.format(\n",
    "    len(training_set_prediction_negative[training_set_prediction_negative == 0]),len(training_set_prediction_negative[training_set_prediction_negative == 1])))\n",
    "print('检测率: {0}, 虚警率: {1}'.format(\n",
    "    (len(training_set_prediction_positive[training_set_prediction_positive == 1]) + 0.0) / len(training_set_prediction_positive),\n",
    "    (len(training_set_prediction_negative[training_set_prediction_negative == 1]) + 0.0) / len(training_set_prediction_negative)\n",
    "))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 在测试集上的结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T12:05:59.140277Z",
     "start_time": "2018-05-07T11:31:32.103Z"
    }
   },
   "outputs": [],
   "source": [
    "test_set = [np.reshape(cv2.cvtColor(cv2.resize(cv2.imread('test8/%06d.png' % i), (256, 256)), cv2.COLOR_BGR2GRAY), (1, 256 * 256)) for i in range(401, 501)]\n",
    "test_set.extend([np.reshape(cv2.cvtColor(cv2.resize(cv2.imread('test8/%06d.png' % i), (256, 256)), cv2.COLOR_BGR2GRAY), (1, 256 * 256)) for i in range(901, 1001)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T12:05:59.142049Z",
     "start_time": "2018-05-07T11:31:32.710Z"
    }
   },
   "outputs": [],
   "source": [
    "test_set_nparray = np.vstack(test_set)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T12:05:59.143972Z",
     "start_time": "2018-05-07T11:31:33.336Z"
    }
   },
   "outputs": [],
   "source": [
    "test_set_prediction = smoke_sess.run(prediction, feed_dict={xs: test_set_nparray})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T12:05:59.145764Z",
     "start_time": "2018-05-07T11:31:33.990Z"
    }
   },
   "outputs": [],
   "source": [
    "test_set_prediction[test_set_prediction >= 0.5] = 1\n",
    "test_set_prediction[test_set_prediction < 0.5] = 0\n",
    "test_set_prediction_positive = test_set_prediction[: 100]\n",
    "test_set_prediction_negative = test_set_prediction[100:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T12:05:59.147750Z",
     "start_time": "2018-05-07T11:31:34.287Z"
    }
   },
   "outputs": [],
   "source": [
    "print('FC Test Set Confusion Matrix\\n          Ture      False')\n",
    "\n",
    "print('Positive： {0}        {1}'.format(\n",
    "    len(test_set_prediction_positive[test_set_prediction_positive == 1]),len(test_set_prediction_positive[test_set_prediction_positive == 0])))\n",
    "print('Negative： {0}        {1}'.format(\n",
    "    len(test_set_prediction_negative[test_set_prediction_negative == 0]),len(test_set_prediction_negative[test_set_prediction_negative == 1])))\n",
    "print('检测率: {0}, 虚警率: {1}'.format(\n",
    "    (len(test_set_prediction_positive[test_set_prediction_positive == 1]) + 0.0) / len(test_set_prediction_positive),\n",
    "    (len(test_set_prediction_negative[test_set_prediction_negative == 1]) + 0.0) / len(test_set_prediction_negative)\n",
    "))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 提取深度特征结合SVM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T12:05:59.149846Z",
     "start_time": "2018-05-07T11:31:36.626Z"
    }
   },
   "outputs": [],
   "source": [
    "from sklearn import svm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T12:05:59.151733Z",
     "start_time": "2018-05-07T11:31:36.791Z"
    }
   },
   "outputs": [],
   "source": [
    "clf = svm.SVC()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T12:05:59.154693Z",
     "start_time": "2018-05-07T11:31:36.944Z"
    }
   },
   "outputs": [],
   "source": [
    "trainging_set_deep_feature = smoke_sess.run(layer1, feed_dict={xs: training_set_nparray})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T12:05:59.158073Z",
     "start_time": "2018-05-07T11:31:37.097Z"
    }
   },
   "outputs": [],
   "source": [
    "clf.fit(trainging_set_deep_feature, np.resize(training_label, (800, )))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T12:05:59.161230Z",
     "start_time": "2018-05-07T11:31:37.274Z"
    }
   },
   "outputs": [],
   "source": [
    "training_set_deep_feature_svm_prediction = clf.predict(trainging_set_deep_feature)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T12:05:59.163454Z",
     "start_time": "2018-05-07T11:31:37.783Z"
    }
   },
   "outputs": [],
   "source": [
    "training_set_deep_feature_svm_prediction[training_set_deep_feature_svm_prediction >= 0.5] = 1\n",
    "training_set_deep_feature_svm_prediction[training_set_deep_feature_svm_prediction < 0.5] = 0\n",
    "training_set_deep_feature_svm_prediction_positive = training_set_deep_feature_svm_prediction[: 400]\n",
    "training_set_deep_feature_svm_prediction_negative = training_set_deep_feature_svm_prediction[400:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T12:05:59.165877Z",
     "start_time": "2018-05-07T11:31:38.694Z"
    }
   },
   "outputs": [],
   "source": [
    "print('FC+SVM Training Set Confusion Matrix\\n          Ture      False')\n",
    "\n",
    "print('Positive： {0}        {1}'.format(\n",
    "    len(training_set_deep_feature_svm_prediction_positive[training_set_deep_feature_svm_prediction_positive == 1]),len(training_set_deep_feature_svm_prediction_positive[training_set_deep_feature_svm_prediction_positive == 0])))\n",
    "print('Negative： {0}        {1}'.format(\n",
    "    len(training_set_deep_feature_svm_prediction_negative[training_set_deep_feature_svm_prediction_negative == 0]),len(training_set_deep_feature_svm_prediction_negative[training_set_deep_feature_svm_prediction_negative == 1])))\n",
    "print('检测率: {0}, 虚警率: {1}'.format(\n",
    "    (len(training_set_deep_feature_svm_prediction_positive[training_set_deep_feature_svm_prediction_positive == 1]) + 0.0) / len(training_set_deep_feature_svm_prediction_positive),\n",
    "    (len(training_set_deep_feature_svm_prediction_negative[training_set_deep_feature_svm_prediction_negative == 1]) + 0.0) / len(training_set_deep_feature_svm_prediction_negative)\n",
    "))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T12:05:59.167933Z",
     "start_time": "2018-05-07T11:31:39.031Z"
    }
   },
   "outputs": [],
   "source": [
    "test_set_deep_feature = smoke_sess.run(layer1, feed_dict={xs: test_set_nparray})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T12:05:59.169913Z",
     "start_time": "2018-05-07T11:31:39.443Z"
    }
   },
   "outputs": [],
   "source": [
    "test_set_deep_feature_svm_prediction = clf.predict(test_set_deep_feature)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T12:05:59.171889Z",
     "start_time": "2018-05-07T11:31:39.935Z"
    }
   },
   "outputs": [],
   "source": [
    "test_set_deep_feature_svm_prediction[test_set_deep_feature_svm_prediction >= 0.5] = 1\n",
    "test_set_deep_feature_svm_prediction[test_set_deep_feature_svm_prediction < 0.5] = 0\n",
    "test_set_deep_feature_svm_prediction_positive = test_set_deep_feature_svm_prediction[: 100]\n",
    "test_set_deep_feature_svm_prediction_negative = test_set_deep_feature_svm_prediction[100:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T12:05:59.173729Z",
     "start_time": "2018-05-07T11:31:40.240Z"
    },
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "print('FC+SVM Test Set Confusion Matrix\\n          Ture      False')\n",
    "\n",
    "print('Positive： {0}        {1}'.format(\n",
    "    len(test_set_deep_feature_svm_prediction_positive[test_set_deep_feature_svm_prediction_positive == 1]),len(test_set_deep_feature_svm_prediction_positive[test_set_deep_feature_svm_prediction_positive == 0])))\n",
    "print('Negative： {0}        {1}'.format(\n",
    "    len(test_set_deep_feature_svm_prediction_negative[test_set_deep_feature_svm_prediction_negative == 0]),len(test_set_deep_feature_svm_prediction_negative[test_set_deep_feature_svm_prediction_negative == 1])))\n",
    "print('检测率: {0}, 虚警率: {1}'.format(\n",
    "    (len(test_set_deep_feature_svm_prediction_positive[test_set_deep_feature_svm_prediction_positive == 1]) + 0.0) / len(test_set_deep_feature_svm_prediction_positive),\n",
    "    (len(test_set_deep_feature_svm_prediction_negative[test_set_deep_feature_svm_prediction_negative == 1]) + 0.0) / len(test_set_deep_feature_svm_prediction_negative)\n",
    "))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda root]",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  },
  "nbpresent": {
   "slides": {
    "1ebacd90-440d-48f1-9dcc-b527d1233433": {
     "id": "1ebacd90-440d-48f1-9dcc-b527d1233433",
     "prev": "f0916fdf-2982-408b-bb38-604734c64134",
     "regions": {
      "bc2e7aba-e06d-499b-9175-86469960a871": {
       "attrs": {
        "height": 0.8,
        "width": 0.8,
        "x": 0.1,
        "y": 0.1
       },
       "content": {
        "cell": "a49f0e91-20ca-4cc7-9b1d-c1847166d96a",
        "part": "whole"
       },
       "id": "bc2e7aba-e06d-499b-9175-86469960a871"
      }
     }
    },
    "2b0b0983-c679-42cf-ab5e-e814e86b0b48": {
     "id": "2b0b0983-c679-42cf-ab5e-e814e86b0b48",
     "prev": "ca820063-a105-4274-af61-2ec63a8d75f9",
     "regions": {
      "97d3056e-5ad9-421a-8d66-4791a4e42495": {
       "attrs": {
        "height": 0.8,
        "width": 0.8,
        "x": 0.1,
        "y": 0.1
       },
       "content": {
        "cell": "62c33fbd-145b-41a9-a977-5eafc6b2aed4",
        "part": "whole"
       },
       "id": "97d3056e-5ad9-421a-8d66-4791a4e42495"
      }
     }
    },
    "36291e9d-496c-455d-9d70-18f1ec7fa121": {
     "id": "36291e9d-496c-455d-9d70-18f1ec7fa121",
     "prev": "a298d5de-e3fb-4bfa-a98c-71daab061781",
     "regions": {
      "08783d10-8ca8-4777-b108-cc8003a4569c": {
       "attrs": {
        "height": 0.8,
        "width": 0.8,
        "x": 0.1,
        "y": 0.1
       },
       "content": {
        "cell": "d5ed983a-19ed-424b-9db7-5a46c19e0af4",
        "part": "whole"
       },
       "id": "08783d10-8ca8-4777-b108-cc8003a4569c"
      }
     }
    },
    "582ba0fc-ae48-4cea-b314-169391e865d2": {
     "id": "582ba0fc-ae48-4cea-b314-169391e865d2",
     "prev": "ee162010-e438-4ebe-98c2-5967e5142fac",
     "regions": {
      "2280d6aa-66e4-45dd-9699-e5f05a6bf654": {
       "attrs": {
        "height": 0.8,
        "width": 0.8,
        "x": 0.1,
        "y": 0.1
       },
       "content": {
        "cell": "5bb7ecd2-f00f-4c04-b7ad-ed441321523c",
        "part": "whole"
       },
       "id": "2280d6aa-66e4-45dd-9699-e5f05a6bf654"
      }
     }
    },
    "6b4369ad-4812-4f72-b424-79fbd94f608a": {
     "id": "6b4369ad-4812-4f72-b424-79fbd94f608a",
     "prev": "e4b5ad32-db30-452c-8e17-e8d134107e8e",
     "regions": {
      "53b2414c-63c8-439e-851e-a5621be12f5b": {
       "attrs": {
        "height": 0.8,
        "width": 0.8,
        "x": 0.1,
        "y": 0.1
       },
       "content": {
        "cell": "28b857a6-1507-4aa3-b102-d291e0748f76",
        "part": "whole"
       },
       "id": "53b2414c-63c8-439e-851e-a5621be12f5b"
      }
     }
    },
    "89de8b8f-984e-42d1-968f-514ec41c46b0": {
     "id": "89de8b8f-984e-42d1-968f-514ec41c46b0",
     "prev": "6b4369ad-4812-4f72-b424-79fbd94f608a",
     "regions": {
      "e23e9eb6-0169-4db0-863a-fb91acb117f4": {
       "attrs": {
        "height": 0.8,
        "width": 0.8,
        "x": 0.1,
        "y": 0.1
       },
       "content": {
        "cell": "d162a062-554a-4c5a-9e93-01a78a9f5710",
        "part": "whole"
       },
       "id": "e23e9eb6-0169-4db0-863a-fb91acb117f4"
      }
     }
    },
    "a298d5de-e3fb-4bfa-a98c-71daab061781": {
     "id": "a298d5de-e3fb-4bfa-a98c-71daab061781",
     "prev": "582ba0fc-ae48-4cea-b314-169391e865d2",
     "regions": {
      "7f17a1aa-6be9-461a-9353-367230f8e323": {
       "attrs": {
        "height": 0.8,
        "width": 0.8,
        "x": 0.1,
        "y": 0.1
       },
       "content": {
        "cell": "7e695e8f-90e7-4695-9b3e-8214afc58ac0",
        "part": "whole"
       },
       "id": "7f17a1aa-6be9-461a-9353-367230f8e323"
      }
     }
    },
    "ca820063-a105-4274-af61-2ec63a8d75f9": {
     "id": "ca820063-a105-4274-af61-2ec63a8d75f9",
     "prev": "cd141fc9-977f-47b7-b293-d9666848bcb3",
     "regions": {
      "01d8ff50-4cfb-4962-9791-2790c6944cd2": {
       "attrs": {
        "height": 0.8,
        "width": 0.8,
        "x": 0.1,
        "y": 0.1
       },
       "content": {
        "cell": "c6d09a75-7d70-46bd-8766-058385563b5d",
        "part": "whole"
       },
       "id": "01d8ff50-4cfb-4962-9791-2790c6944cd2"
      }
     }
    },
    "cd141fc9-977f-47b7-b293-d9666848bcb3": {
     "id": "cd141fc9-977f-47b7-b293-d9666848bcb3",
     "prev": null,
     "regions": {
      "330b3d31-a401-4352-91cc-0526c003d41c": {
       "attrs": {
        "height": 0.8,
        "width": 0.8,
        "x": 0.1,
        "y": 0.1
       },
       "content": {
        "cell": "35a12da4-0c55-40d8-b81a-55e8ac4ca4fd",
        "part": "whole"
       },
       "id": "330b3d31-a401-4352-91cc-0526c003d41c"
      }
     }
    },
    "d8517b82-b87a-491c-bf0a-5919241a61f5": {
     "id": "d8517b82-b87a-491c-bf0a-5919241a61f5",
     "prev": "1ebacd90-440d-48f1-9dcc-b527d1233433",
     "regions": {
      "86d47367-21f4-4941-9d27-bc8cf9c57398": {
       "attrs": {
        "height": 0.8,
        "width": 0.8,
        "x": 0.1,
        "y": 0.1
       },
       "content": {
        "cell": "0aeda3fb-3213-4aca-81d0-16a5463ce011",
        "part": "whole"
       },
       "id": "86d47367-21f4-4941-9d27-bc8cf9c57398"
      }
     }
    },
    "e4b5ad32-db30-452c-8e17-e8d134107e8e": {
     "id": "e4b5ad32-db30-452c-8e17-e8d134107e8e",
     "prev": "d8517b82-b87a-491c-bf0a-5919241a61f5",
     "regions": {
      "c2a01682-19b7-4d77-9865-36d0fc3d17b8": {
       "attrs": {
        "height": 0.8,
        "width": 0.8,
        "x": 0.1,
        "y": 0.1
       },
       "content": {
        "cell": "90a8e83d-c1de-4917-8510-4eccf6eb4c20",
        "part": "whole"
       },
       "id": "c2a01682-19b7-4d77-9865-36d0fc3d17b8"
      }
     }
    },
    "ee162010-e438-4ebe-98c2-5967e5142fac": {
     "id": "ee162010-e438-4ebe-98c2-5967e5142fac",
     "prev": "ff82fbf7-5989-4f88-802b-56d016808768",
     "regions": {
      "a39dea0b-6cce-4a7a-a8aa-aff648943905": {
       "attrs": {
        "height": 0.8,
        "width": 0.8,
        "x": 0.1,
        "y": 0.1
       },
       "content": {
        "cell": "d6710ce8-43df-459c-b9c3-7d4bcd29337f",
        "part": "whole"
       },
       "id": "a39dea0b-6cce-4a7a-a8aa-aff648943905"
      }
     }
    },
    "f0916fdf-2982-408b-bb38-604734c64134": {
     "id": "f0916fdf-2982-408b-bb38-604734c64134",
     "prev": "36291e9d-496c-455d-9d70-18f1ec7fa121",
     "regions": {
      "77483dc1-e0eb-4178-8ba8-e5a9fd2ce966": {
       "attrs": {
        "height": 0.8,
        "width": 0.8,
        "x": 0.1,
        "y": 0.1
       },
       "content": {
        "cell": "a2497bd6-b9cc-46d0-9c13-3b9cff10586a",
        "part": "whole"
       },
       "id": "77483dc1-e0eb-4178-8ba8-e5a9fd2ce966"
      }
     }
    },
    "ff82fbf7-5989-4f88-802b-56d016808768": {
     "id": "ff82fbf7-5989-4f88-802b-56d016808768",
     "prev": "2b0b0983-c679-42cf-ab5e-e814e86b0b48",
     "regions": {
      "9d792f9d-aa33-4448-bfca-41396e166278": {
       "attrs": {
        "height": 0.8,
        "width": 0.8,
        "x": 0.1,
        "y": 0.1
       },
       "content": {
        "cell": "917b1adc-6238-438f-9003-9347bf44f1a4",
        "part": "whole"
       },
       "id": "9d792f9d-aa33-4448-bfca-41396e166278"
      }
     }
    }
   },
   "themes": {}
  },
  "toc": {
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": "block",
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
